import { _decorator, Component, instantiate, Node, Prefab } from 'cc';
import { GameWeapon } from './GameWeapon';
import { Gun } from './Gun';
import PlayerController from '../PlayerController';
import { Bullet } from './Bullet';
import { GameData } from './GameData';
import { UITYPE, WEAPON } from '../AshFramework/Datas/Constant';
import { ZTool } from '../AshFramework/Utils/ZTool';
import { BundleManager, Bundles } from '../AshFramework/Managers/BundleManager';
import { GameManager } from './GameManager';
import { HintController } from './HintController';
import { ResourceUtil } from '../AshFramework/Utils/ResourceUtil';
import { BulletController_SLD } from './BulletController_SLD';
import { EventManager, MyEvent } from '../AshFramework/Managers/EventManager';
import { TLWLSJ_UIManager } from './UIManager';
const { ccclass, property } = _decorator;

@ccclass('GameWeapon_GBSL')
export class GameWeapon_GBSL extends GameWeapon {

    Muzzle: Node = null;
    private _data: Gun = null;
    private _bullets: Bullet[] = [];
    private _capacity: number = 0;

    private _num: number = 0;
    private _weaponName: string = "";
    protected start(): void {
        this.Muzzle = this.node.getChildByName("枪口");
        this._data = this.Data as Gun;
        PlayerController.Instance.WeaponTs = this;
        this._weaponName = ZTool.GetEnumKeyByValue(WEAPON, this.WeaponType);
        this._num = GameData.getBulletNumByName(this._weaponName);
        this.scheduleOnce(() => this.updateWeaponShowByNum(this._num), 0.1)
    }

    attack() {
        if (!this.isAttack()) return;
        if (this.IsStop) {
            return;
        }
        if (this._num > 0) {
            this.IsFire = true;
            this._num--;
            this.fireBullet();
            GameData.subBulletByName(this._weaponName);
            this.updateWeaponShowByNum(this._num);
            this.attackAni(1, () => {
                this.IsFire = false;
                this.attack();
            })
        } else {
            TLWLSJ_UIManager.Instance.showDXPanel(UITYPE.商店, () => {
                TLWLSJ_UIManager.Instance.closeDXPanel();
                GameData.addBulletByName(this._weaponName, 10);
                this.scheduleOnce(() => { this.updateNum(); }, 0.1);
            })
            this.endAttack();
        }
    }

    attackAni(time: number, cb: Function = null) {
        this.scheduleOnce(() => {
            cb && cb();
        }, time);
    }

    //发射子弹
    fireBullet() {
        BundleManager.LoadPrefab(Bundles.Prefabs, "子弹_高爆手榴弹").then((prefab: Prefab) => {
            const bullet: Node = instantiate(prefab);
            bullet.parent = GameManager.Instance.BulletLayout;
            bullet.setWorldPosition(this.Muzzle.getWorldPosition());
            bullet.getComponent(BulletController_SLD).fire(this._weaponName, PlayerController.Instance.DirX, PlayerController.Instance.DirY);
        });
    }

    updateNum() {
        this._num = GameData.getBulletNumByName(this._weaponName);
        this.updateWeaponShowByNum(this._num);
    }

    protected onEnable(): void {
        super.onEnable();
        EventManager.on(MyEvent.UPDATESLD, this.updateNum, this);
    }

    protected onDisable(): void {
        super.onDisable();
        EventManager.off(MyEvent.UPDATESLD, this.updateNum, this);
    }

}


